package com.itheima.organization.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.aom.pojo.AomOrder;
import com.itheima.aom.pojo.AomUser;
import com.itheima.aom.pojo.QueryPageBean;
import com.itheima.organization.dao.AomMapper;
import com.itheima.organization.dao.AomUserMapper;
import com.itheima.organization.service.AomUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class AomUserServiceImpl implements AomUserService {

    @Autowired
    private AomUserMapper aomUserMapper;
    @Autowired
    private AomMapper aomMapper;


    @Override
    public AomUser findById(Long id) {
        AomUser aomUser = aomUserMapper.selectByPrimaryKey(id);
        return aomUser;
    }

    /**
     * 新增用户
     *
     * @param aomUser
     */
    @Override
    public void add(AomUser aomUser) {
        aomUserMapper.insertSelective(aomUser);
    }

    /**
     * 修改用户
     *
     * @param aomUser
     */
    @Override
    public void update(AomUser aomUser) {
        aomUserMapper.updateByPrimaryKeySelective(aomUser);
    }

    /**
     * 分页查询用户
     * @param map
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<AomUser> queryAomUser(Map<String, Object> map, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        Example example = createExample(map);
        List<AomUser> aomUsers = aomUserMapper.selectByExample(example);
        return (Page<AomUser>) aomUsers;
    }

    /**
     * 重置密码
     * @param aomUser
     */
    @Override
    public void updatePassword(AomUser aomUser) {

        //parentid 不为空的用户 不是农场主账号
        if (this.findById(aomUser.getId()).getParentid() != null){
            throw new RuntimeException("无权重置此账号密码");
        }
        AomUser aomUser1 = new AomUser();
        aomUser1.setId(aomUser.getId());
        aomUser1.setPassword("123456");
        aomUserMapper.updateByPrimaryKeySelective(aomUser1);

    }

    private Example createExample(Map<String, Object> searchMap){
        Example example = new Example(AomUser.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            //农业组织用户id
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            if (searchMap.get("username") != null && !"".equals(searchMap.get("username"))) {
                criteria.andEqualTo("username", searchMap.get("username"));
            }
            if (searchMap.get("aomid") != null && !"".equals(searchMap.get("aomid"))) {
                criteria.andEqualTo("aomid", searchMap.get("aomid"));
            }
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }
            if (searchMap.get("firstlogin") != null && !"".equals(searchMap.get("firstlogin"))) {
                criteria.andEqualTo("firstlogin", searchMap.get("firstlogin"));
            }
        }
        return example;
    }
}
